home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group00a.txt
/
000017_icon-group-sender _Mon Jan 31 14:09:03 2000.msg
< prev
next >
Wrap
Internet Message Format
|
2001-01-03
|
4KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id OAA07066
for icon-group-addresses; Mon, 31 Jan 2000 14:07:28 -0700 (MST)
Message-Id: <200001312107.OAA07066@baskerville.CS.Arizona.EDU>
Date: Mon, 31 Jan 2000 13:50:15 -0600
From: "Charles Hethcoat" <CHETHCOA@oss.oceaneering.com>
To: <icon-group@optima.CS.Arizona.EDU>
Subject: Updating array parameters in Icon (a mini-dissertation)
Content-Disposition: inline
X-Guinevere: 1.0.12 ; Oceaneering Int'l
X-MIME-Autoconverted: from quoted-printable to 8bit by baskerville.CS.Arizona.EDU id MAA04310
Errors-To: icon-group-errors@optima.CS.Arizona.EDU
Status: RO
I have an Icon programming problem that needs clarification, but I
suspect many Icon programmers can benefit from what I've found out about
it so far.
BACKGROUND
I have from time to time needed to write a procedure that returns a list
as its value. Example:
list_value := func_1()
However, sometimes the list is an update of values in an already
existing list. This occurs frequently in numerical analysis. Example:
list_value := list(1, 10) # Creating the list
...
list_value := func_2(list_value) # Returning a new list
The returned value of func_2() is based on a computation using the
initial value of list_value as input to an algorithm that calculates a
vector of deltas and adds them to the original list. This updated list
is the function result.
But once in a while it turns out that another value is a better
candidate for the returned value. The list in question then becomes a
parameter to the procedure. Example:
list_value := list(1, 10) # Creating the list
...
if func_3(list_value) then # Updating the list
# success:
x := list_value[1] # (x ~= 1) # Using the list
else
# failure case...
Here, func_3() updates the list_value parameter, sets some return value,
and returns, so that the updated list is ready for further use.
I knew that a scalar value, when assigned to a parameter inside a
procedure, would not survive return to the caller. I also knew that a
record data structure could be so updated. (I have even hidden a scalar
value inside a record to get around this restriction.) But I wasn't sure
about lists. I noticed that sometimes the list would come back updated,
and sometimes it wouldn't.
STRANGE BEAST ISOLATED IN CAPTIVITY!
I finally discovered that a list parameter can indeed be updated, but
you have to update it in a certain way. I wrote the following program to
illustrate the problem and the solution:
========8<==================8<====================
# Verify behavior of updated list parameters
procedure main()
local x, y
x := []
y := []
write("Before updating:")
write("x[1] = >", (x[1] | "empty"), "<")
write("y[1] = >", (y[1] | "empty"), "<")
method1(x)
method2(y)
write("After updating:")
write("x[1] = >", (x[1] | "empty"), "<")
write("y[1] = >", (y[1] | "empty"), "<")
exit()
end
# method1 - assign a new list value to the parameter
procedure method1(x)
x := [&e]
return
end
# method2 - put a new value on the existing list
procedure method2(x)
put(x, &pi)
return
end
========8<==================8<====================
The output of this program is
========8<==================8<====================
Before updating:
x[1] = >empty<
y[1] = >empty<
After updating:
x[1] = >empty<
y[1] = >3.141592653589793<
========8<==================8<====================
QUESTION
I suspect that the answer has to do with the pointer semantics of Icon,
but if anyone can flesh this out for me I'd appreciate it. Meanwhile,
maybe some will find this a useful programming tip.
Thanks in advance,
Charles Hethcoat
Sr. Engineer
Oceaneering Space Systems
Houston, TX 77058 USA